home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / support / copydb.q next >
Encoding:
Text File  |  1985-03-06  |  7.1 KB  |  414 lines

  1. # include    <stdio.h>
  2. # include    <useful.h>
  3. # include    <sccs.h>
  4.  
  5. SCCSID(@(#)copydb.q    8.2    1/17/85)
  6.  
  7.  
  8. # define    MAXNAME    13    /* one more for the null */
  9. # define    MAXKEYS    6    /* changing this is insuficient */
  10. # define    MAXINDEX    10    /* change as needed */
  11. # define    MAXREL    1000    /* can be increased as needed */
  12. # define    MAXDOM     50
  13. # define    INT    30
  14. # define    FLOAT    31
  15. # define    CHAR    32
  16. ##char    Usercode[100];
  17. char    **Wanted;
  18. int    Status;
  19.  
  20. extern    int    (*ExitFn)();
  21. FILE    *Ifile, *Ofile;
  22.  
  23. struct relation
  24. {
  25.     char    name[MAXNAME];
  26.     char    indx;
  27.     int    spec;
  28.     int    atts;
  29. };
  30.  
  31. struct attribute
  32. {
  33.     char    aname[MAXNAME];
  34.     char    format;
  35.     int    length;
  36. };
  37.  
  38. struct index
  39. {
  40.     char    iname[MAXNAME];
  41.     char    ispec;
  42.     char    idom[MAXKEYS];
  43. };
  44.  
  45.  
  46. /*
  47. ** sysexit
  48. **    The function to call for an exit to occur, this
  49. **    is all for the benefit of syserr, so it doesn't core dump.
  50. */
  51. sysexit(value)
  52. int    value;
  53. {
  54.     exit(value);
  55. }/* sysexit */
  56.  
  57. main(argc,argv)
  58. int    argc;
  59. char    **argv;
  60. {
  61.  
  62. ##    char    *db;
  63.     char    *path;
  64. ##    char    name[MAXNAME];
  65. ##    int    indx;
  66. ##    int    atts;
  67. ##    int    spec;
  68. ##    char    iname[MAXNAME];
  69. ##    int    idomv1, idomv2, idomv3, idomv4, idomv5, idomv6;
  70.  
  71. ##    int    n;
  72.  
  73.     register struct relation    *relptr;
  74.     register struct attribute    *attptr;
  75.     register int            i;
  76.     struct relation            relations[MAXREL];
  77.     struct attribute        attributes[MAXDOM], indatts[MAXKEYS];
  78.     struct index            indexes[MAXINDEX];
  79.     struct index            *indxptr;
  80.     int                attxtra[MAXDOM], indattx[MAXKEYS];
  81.     int                xcount;
  82.     int                j;
  83.     char                line[100];
  84. ##    char                *uover;
  85.  
  86.     umask(022);
  87.     ExitFn = sysexit;        /* set up exit routine for syserr */
  88.  
  89.     if (argc < 3)
  90.         syserr(0,"usage: copydb database targetdir [relation] ...");
  91.  
  92.     uover = 0;
  93.     if (bequal(*++argv, "-u", 2))
  94.         uover = *argv++;
  95.     db = *argv++;
  96.     path = *argv++;
  97.     Wanted = argv++;
  98.  
  99.     if(path[0] != '/')
  100.         printf("Warning %s is not a full path name\n",path);
  101.  
  102.  
  103.     if ((Ifile = fopen(ztack(path,"/copy.in"), "w")) == NULL)
  104.         syserr(0,"Cannot create %s!",ztack(path,"/copy.in"));
  105.     if ((Ofile = fopen(ztack(path,"/copy.out"), "w")) == NULL)
  106.         syserr(0,"Cannot create %s!",ztack(path,"/copy.out"));
  107.  
  108.  
  109. ##    ingres    db uover
  110.  
  111. ##    retrieve (Usercode = usercode)
  112.  
  113. ##    range of r is relation
  114.     i = 0;
  115.  
  116.     /*
  117.     **    Bring the relations into core
  118.     */
  119.     relptr = relations;
  120. ##    retrieve (name = r.relid, spec = r.relspec, indx = r.relindxd,
  121. ##        atts = r.relatts)
  122. ##        where r.relowner = Usercode
  123. ##        and mod(r.relstat,2) != 1 /* sys catalog */
  124. ##        and r.relindxd >= 0
  125. ##        and r.relid != "_SYS*" /* system temporaries */
  126. ##    {
  127.         if (notwanted(name))
  128.             continue;
  129.         if(i++>=MAXREL)
  130.             syserr("Too many relations!!!");
  131.         smove(name,(relptr->name));
  132.         relptr->spec = spec;
  133.         relptr->indx = indx;
  134.         relptr++->atts = atts;
  135. ##    }
  136.  
  137.  
  138.     /*
  139.     **    For each relation bring its attributes into core
  140.     **    in domain order
  141.     */
  142. ##    range of a is attribute
  143. ##    range of i is indexes
  144.     n = i;
  145.     printf("%d relations found\n",n);
  146.     for (relptr = relations; relptr < &relations[n]; relptr++)
  147.     {
  148.         writein("create ");
  149.         writeout("copy ");
  150.         smove(relptr->name,name);
  151.         writeboth(name);
  152.         writeboth("(\n\t");
  153.  
  154.         xcount = retatts(name,attributes,attxtra);
  155.         for(i = 0; i < relptr->atts; i++)
  156.         {
  157.             attptr = &attributes[i];
  158.             if(i)
  159.                 writeboth(",\n\t");
  160.             writeboth(attptr->aname);
  161.             putboth('=');
  162.             switch(attptr->format)
  163.             {
  164.  
  165.               case 'i':
  166.                 putboth('i');
  167.                 break;
  168.  
  169.               case 'f':
  170.                 putboth('f');
  171.                 break;
  172.  
  173.               case 'c':
  174.                 putboth('c');
  175.                 break;
  176.  
  177.               default:
  178.                 syserr("Bad type: %d, in %c of %s!!",
  179.                     attptr->format,attptr->aname,
  180.                     name);
  181.  
  182.             }
  183.                 attptr->length &= I1MASK;
  184.                 writeboth(iocv(attptr->length));
  185.         }
  186.  
  187.         writeboth("\n)");
  188.         writeout(" into \"");
  189.         writein("\n\\p\\g\ncopy ");
  190.         writein(name);
  191.         writein("() from \"");
  192.         writeboth(path);
  193.         putboth('/');
  194.         writeboth(name);
  195.         writeboth(Usercode);
  196.         writeboth("\"\n\\p\\g\n");
  197.  
  198.  
  199.         if (relptr->spec != 5 && relptr->spec != 0)
  200.             modify(name,attributes,attxtra,relptr->spec,xcount);
  201.         if (relptr->indx)
  202.         {
  203.             indxptr = indexes;
  204.             i = 0;
  205.  
  206. ##            retrieve (iname = i.irelidi, spec = i.irelspeci,
  207. ##                idomv1 = i.idom1, idomv2 = i.idom2, idomv3 = i.idom3,
  208. ##                idomv4 = i.idom4, idomv5 = i.idom5, idomv6 = i.idom6)
  209. ##            where i.irelidp = name and i.iownerp = Usercode
  210. ##            {
  211.  
  212.                 if (i++ >= MAXINDEX)
  213.                     syserr("Too many indexes on %s!!",name);
  214.                 smove(iname,indxptr->iname);
  215.                 indxptr->ispec = spec;
  216.                 indxptr->idom[0] = idomv1;
  217.                 indxptr->idom[1] = idomv2;
  218.                 indxptr->idom[2] = idomv3;
  219.                 indxptr->idom[3] = idomv4;
  220.                 indxptr->idom[4] = idomv5;
  221.                 indxptr->idom[5] = idomv6;
  222.                 indxptr++;
  223. ##            }
  224.  
  225.             while (--indxptr >= indexes)
  226.             {
  227.     
  228.                 writein("index on ");
  229.                 writein(name);
  230.                 writein(" is ");
  231.                 writein(indxptr->iname);
  232.                 writein("(\n\t");
  233.     
  234.                 for (i=0; indxptr->idom[i] && i < MAXKEYS; i++)
  235.                 {
  236.                     if(i)
  237.                         writein(",\n\t");
  238.                     writein(attributes[indxptr->idom[i]-1].aname);
  239.                 }
  240.                 writein(")\n\\p\\g\n");
  241.                 if(indxptr->ispec != 10 && indxptr->ispec != 11)
  242.                 {
  243.                     xcount = retatts(indxptr->iname,indatts,indattx);
  244.                     modify(indxptr->iname,indatts,indattx,indxptr->ispec,xcount);
  245.                 }
  246.             }
  247.         }
  248.     }
  249.     fflush(Ifile);
  250.     fflush(Ofile);
  251.     printf("All done!\n\n");
  252. }
  253. exitfn()
  254. {
  255.     fflush(Ifile);
  256.     fflush(Ofile);
  257.     exit(-1);
  258. }
  259.  
  260.  
  261. modify(name, attributes, attxtra, spec, xcount)
  262. char    *name;
  263. struct attribute    *attributes;
  264. int    *attxtra;
  265. int    spec;
  266. int    xcount;
  267. {
  268.     register    i,j;
  269.     writein("modify ");
  270.     writein(name);
  271.     writein(" to ");
  272.     switch(spec)
  273.     {
  274.  
  275.       case -5:
  276.         writein("cheap");
  277.         goto nokeys;
  278.  
  279.       case -10:
  280.       case -11:
  281.         writein("c");
  282.       case 10:
  283.       case 11:
  284.         writein("isam on ");
  285.         break;
  286.  
  287.       case -20:
  288.       case -21:
  289.         writein("c");
  290.       case 21:
  291.       case 20:
  292.         writein("hash on ");
  293.         break;
  294.  
  295.       default:
  296.         syserr("Unimplimented spec: %d on %s",
  297.             spec,name);
  298.  
  299.     }
  300.     j = 0;
  301.     for (i = 1; i <= xcount; i++)
  302.     {
  303.         if(j++)
  304.             writein(",\n\t");
  305.         writein(attributes[attxtra[i]].aname);
  306.     }
  307.     if (!j)
  308.         syserr("No keys found for %s!!!",name);
  309. nokeys:
  310.     writein("\n\\p\\g\n");
  311. }
  312. retatts(name,attributes,attxtra)
  313. char    *name;
  314. struct attribute    *attributes;
  315. int    attxtra[];
  316. {
  317.     register    xcount, i;
  318.     register struct attribute    *attptr;
  319.  
  320. ##    char    aname[MAXNAME];
  321. ##    int    domain, length, extra;
  322. ##    char    format[1];
  323.     i = xcount = 0;
  324. ##    retrieve (aname = a.attname,domain = a.attid, format = a.attfrmt,
  325. ##         length = a.attfrml, extra = a.attxtra)
  326. ##
  327. ##    where a.attrelid = name and a.attowner = Usercode
  328. ##    {
  329.         if (i++ >= MAXDOM)
  330.             syserr("Too many attributes!!!");
  331.         attptr = &attributes[domain-1];
  332.         smove(aname,attptr->aname);
  333.         attptr->format = format[0];
  334.         attptr->length = length;
  335.         if(extra)
  336.         {
  337.             attxtra[extra] = domain - 1;
  338.             xcount++;
  339.         }
  340. ##    }
  341.     return(xcount);
  342. }
  343.  
  344.  
  345. writeboth(string)
  346. char    *string;
  347. {
  348.     register char    *sp;
  349.  
  350.     sp = string;
  351.  
  352.     while(*sp)
  353.     {
  354.         putc(*sp, Ofile);
  355.         putc(*sp++, Ifile);
  356.     }
  357. }
  358.  
  359. putboth(ch)
  360. char    ch;
  361. {
  362.     putc(ch, Ofile);
  363.     putc(ch, Ifile);
  364. }
  365.  
  366.  
  367. writeout(string)
  368. char    *string;
  369. {
  370.     register char     *sp;
  371.  
  372.     sp = string;
  373.     while (*sp)
  374.         putc(*sp++,Ofile);
  375. }
  376.  
  377.  
  378. writein(string)
  379. char    *string;
  380. {
  381.     register char    *sp;
  382.  
  383.     sp = string;
  384.     while (*sp)
  385.         putc(*sp++,Ifile);
  386. }
  387.  
  388.  
  389.  
  390.  
  391. /*
  392. ** Check to see if relation is wanted.
  393. */
  394.  
  395.  
  396. notwanted(relname)
  397. char    *relname;
  398.  
  399. {
  400.  
  401.     register char    **wantlist;
  402.     register char    *nptr;
  403.  
  404.     if (*(wantlist = Wanted) == (char *) 0)
  405.         return (0);
  406.  
  407.     nptr = relname;
  408.     do
  409.         if (!scompare(nptr, 0, *wantlist++, 0))
  410.             return (0);
  411.     while (*wantlist != (char *) 0);
  412.     return (1);
  413. }
  414.